package com.play.ballen.home.adapter

import android.annotation.SuppressLint
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemChildClickListener
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.play.ballen.R
import com.play.ballen.event.EventCost
import com.play.ballen.home.entity.Comment
import com.play.ballen.widget.emoji.FaceTextUtil
import com.yuven.appframework.util.imageloader.ImageUtil
import com.yuven.baselib.component.eventbus.Event
import com.yuven.baselib.component.eventbus.EventBusUtil
import com.yuven.baselib.utils.ResourceUtil

class CommentListAdapter(data: MutableList<Comment>) :
    BaseQuickAdapter<Comment, BaseViewHolder>(R.layout.comment_list_item_layout, data) {
    init {
        addChildClickViewIds(R.id.huifu,R.id.zan_ly)
    }

    private val adapter: CommentList2Adapter by lazy {
        CommentList2Adapter(arrayListOf())
    }

    fun addItemAtBeginning(item: Comment) {
        data.add(0, item)
        notifyItemInserted(0)
    }
    @SuppressLint("ResourceType")
    override fun convert(holder: BaseViewHolder, item: Comment) {
        ImageUtil.instance.loadCircleImage(item.avatar_url, holder.getView(R.id.iv_head) , 0, 0)
        holder.setText(R.id.commentMember, item.nickname)
        holder.setText(R.id.message, FaceTextUtil.renderVideoComment(item.content,""))
        holder.setText(R.id.addTime, (item.post_time))
        holder.getView<RecyclerView>(R.id.rv_comment2).layoutManager =  LinearLayoutManager(context)
        holder.setText(R.id.zan_tv, item.like_count.toString())

        if(item.like_count>0){
            holder.getView<TextView>(R.id.zan_tv).visibility = View.VISIBLE
        }else{
            holder.getView<TextView>(R.id.zan_tv).visibility = View.INVISIBLE
        }
        if(item.is_like){
            holder.getView<ImageView>(R.id.zan).background = ResourceUtil.getDrawable(R.drawable.zanle)
        }else{
            holder.getView<ImageView>(R.id.zan).background = ResourceUtil.getDrawable(R.drawable.zan)
        }

        if (item.comment.size > 3) {
            var subAdapter = CommentList2Adapter(item.comment.subList(0, 3))
            holder.setText(R.id.tv_show_all, "显示全部"+item.comment.size)
            holder.getView<TextView>(R.id.tv_show_all).visibility = View.VISIBLE
            holder.getView<RecyclerView>(R.id.rv_comment2).adapter = subAdapter
            holder.getView<TextView>(R.id.tv_show_all).setOnClickListener {
                // Toggle between showing all and showing only 3 items
                val isShowingAll = holder.getView<TextView>(R.id.tv_show_all).text.contains("显示全部")
                if (isShowingAll) {
                    holder.getView<TextView>(R.id.tv_show_all).text = "折叠"
                    subAdapter.setNewData(item.comment)
                } else {
                    holder.getView<TextView>(R.id.tv_show_all).text = "显示全部"+item.comment.size
                    subAdapter.setNewData(item.comment.subList(0, 3))
                }
            }
            subAdapter.setOnItemChildClickListener(object : OnItemChildClickListener {
                @SuppressLint("NotifyDataSetChanged")
                override fun onItemChildClick(
                    adapter: BaseQuickAdapter<*, *>,
                    view: View,
                    position: Int
                ) {
                    when (view.getId()) {
                        R.id.huifu -> {
                            EventBusUtil.post(Event.create(EventCost.REPLY_COMMENT_2,
                                item.comment[position]))
                        }
                        R.id.zan_ly -> {
                            if(item.comment[position].is_like){
                                item.comment[position].is_like = false
                                item.comment[position].like_count-=1
                                subAdapter.notifyDataSetChanged()
                                EventBusUtil.post(Event.create(EventCost.REPLY_COMMENT_UNLike,
                                    item.comment[position].id))
                            }else{
                                item.comment[position].is_like =true
                                item.comment[position].like_count+=1
                                subAdapter.notifyDataSetChanged()
                                EventBusUtil.post(Event.create(EventCost.REPLY_COMMENT_Like,
                                    item.comment[position].id))
                            }
                        }
                    }
                }
            })
            subAdapter.setOnItemLongClickListener() { _, view, position ->
                EventBusUtil.post(Event.create(EventCost.ADD_REPORT,
                    item.comment[position]))
                true
            }

        } else {
            val subAdapter = CommentList2Adapter(item.comment)
            holder.getView<RecyclerView>(R.id.rv_comment2).adapter = subAdapter
            holder.getView<TextView>(R.id.tv_show_all).visibility = View.GONE
            subAdapter.setOnItemChildClickListener(object : OnItemChildClickListener {
                override fun onItemChildClick(
                    adapter: BaseQuickAdapter<*, *>,
                    view: View,
                    position: Int
                ) {
                    when (view.getId()) {
                        R.id.huifu -> {
                            EventBusUtil.post(Event.create(EventCost.REPLY_COMMENT_2,
                                item.comment[position]))
                        }
                        R.id.zan_ly -> {
                            if(item.comment[position].is_like){
                                item.comment[position].is_like =false
                                item.comment[position].like_count-=1
                                subAdapter.notifyDataSetChanged()
                                EventBusUtil.post(Event.create(EventCost.REPLY_COMMENT_UNLike,
                                    item.comment[position].id))
                            }else{
                                item.comment[position].is_like =true
                                item.comment[position].like_count+=1
                                subAdapter.notifyDataSetChanged()
                                EventBusUtil.post(Event.create(EventCost.REPLY_COMMENT_Like,
                                    item.comment[position].id))
                            }
                        }
                    }
                }
            })

            subAdapter.setOnItemLongClickListener() { _, view, position ->
                EventBusUtil.post(Event.create(EventCost.ADD_REPORT,
                    item.comment[position]))
                true
            }
        }
    }
}

